﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Configuration;

namespace OBUActivation.LOG
{
    class Log
    {

        private const int LOG_DEBUG = 1;

        private const int LOG_INFO = 2;

        private const int LOG_WRAN = 3;

        private const int LOG_ERROR = 4;

        private const int NOTHING = 5;

        public static int LEVEL = LOG_DEBUG;

        private static Object _lock = new Object();

        private static FileInfo fileInfo = null;

        private static string LOG_PATH_KEY = "logPath";

        private Log()
        {
        }


        /// <summary>
        /// Debug类型的log
        /// </summary>
        /// <param name="log"></param>
        public static void d(String log)
        {

            lock (_lock)
            {

                if (LEVEL <= LOG_DEBUG)
                {

                    FileStream fs = getLogFile();
                    if (fs != null)
                    {
                        writeLog(LOG_DEBUG, log, new StreamWriter(fs));
                    }
                }
            }

        }

        /// <summary>
        /// INFO类型的log
        /// </summary>
        /// <param name="log"></param>
        public static void i(String log)
        {

            lock (_lock)
            {

                if (LEVEL <= LOG_DEBUG)
                {

                    FileStream fs = getLogFile();
                    if (fs != null)
                    {
                        writeLog(LOG_INFO, log, new StreamWriter(fs));
                    }
                }
            }

        }

        /// <summary>
        /// Wran类型的log
        /// </summary>
        /// <param name="log"></param>
        public static void w(String log)
        {

            lock (_lock)
            {

                if (LEVEL <= LOG_DEBUG)
                {

                    FileStream fs = getLogFile();
                    if (fs != null)
                    {
                        writeLog(LOG_WRAN, log, new StreamWriter(fs));
                    }
                }
            }

        }

        /// <summary>
        /// Error类型的log
        /// </summary>
        /// <param name="log"></param>
        public static void e(String log)
        {

            lock (_lock)
            {

                if (LEVEL <= LOG_DEBUG)
                {

                    FileStream fs = getLogFile();
                    if (fs != null)
                    {
                        writeLog(LOG_ERROR, log, new StreamWriter(fs));
                    }
                }
            }

        }

        /// <summary>
        /// 获取log文件
        /// </summary>
        /// <param name="log"></param>
        private static FileStream getLogFile()
        {
            FileStream fs = null;

            try
            {
                DateTime now = DateTime.Now;
                string logPath = ConfigurationManager.AppSettings[LOG_PATH_KEY];
                string logName = System.DateTime.Now.ToString("yyyyMMdd") + "log.txt";
                if (logPath.Equals(""))
                {

                    DirectoryInfo dInfo = new DirectoryInfo("log");
                    if (!dInfo.Exists)
                    {
                        dInfo.Create();
                    }
                    fileInfo = new FileInfo("log" + "\\" + logName);
                }
                else
                {
                    DirectoryInfo dInfo = new DirectoryInfo(@logPath);
                    if (!dInfo.Exists)
                    {
                        dInfo.Create();
                    }
                    fileInfo = new FileInfo(@logPath + "\\" + logName);
                }

                if (!fileInfo.Exists)
                {
                    fs = new FileStream(fileInfo.FullName, FileMode.CreateNew);
                }
                else
                {
                    fs = new FileStream(fileInfo.FullName, FileMode.Append);
                }



            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());

            }

            return fs;
        }

        /// <summary>
        /// 写log信息
        /// </summary>
        /// <param name="level">log级别</param>
        /// <param name="log">log信息</param>
        /// <param name="sw">StreamWriter对象</param>
        private static void writeLog(int level, string log, StreamWriter sw)
        {

            string content = "";

            DateTime now = System.DateTime.Now;

            content += now.ToString("HH:mm:ss ");

            switch (level)
            {
                case LOG_DEBUG:

                    content += "DEBUG\\ ";

                    break;

                case LOG_INFO:

                    content += "INFO\\ ";

                    break;

                case LOG_WRAN:

                    content += "WRAN\\ ";
                    break;

                case LOG_ERROR:
                    content += "ERROR\\ ";
                    break;
            }

            content += log;

            sw.WriteLine(content);
            sw.Close();
        }
    }
}
